package org.bluesky.controller;

import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.dto.LinkDto;
import org.bluesky.framework.entity.Link;
import org.bluesky.framework.service.LinkService;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.vo.LinkVo;
import org.bluesky.framework.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content/link")
public class LinkController {

    @Autowired
    private LinkService linkService;

    @GetMapping("/list")
    public ResponseResult list(LinkDto linkDto,Integer pageNum,Integer pageSize){
        PageVo page = linkService.getLinkList(linkDto, pageNum, pageSize);
        return  ResponseResult.okResult(page);
    }

    @PostMapping
    public ResponseResult addLink(@RequestBody LinkDto linkDto){
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        linkService.save(link);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult getLinkById(@PathVariable("id")Long id){
        Link link = linkService.getById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @PutMapping
    public ResponseResult updateLink(@RequestBody LinkDto linkDto){
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        linkService.updateById(link);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteLinkById(@PathVariable("id") Long id){
        linkService.removeById(id);
        return ResponseResult.okResult();
    }
}
